import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

/**
 * @author Harm Kevin
 *
 */
class DownloadURL {

	ArrayList<String> htmlList = new ArrayList<String>();

	public ArrayList<String> downloadURL(URL url, String domain) throws IOException {
		URLConnection conn = url.openConnection();
		String type = conn.getContentType();
		ArrayList<String> html = new ArrayList<String>();
		HttpURLConnection huc = (HttpURLConnection)conn;
		if(huc.getResponseCode() != 200) return html;
		
		

		String urlpath = url.toString();
		urlpath = urlpath.replace(domain, "");

		
		String urlparts[] = urlpath.split("/");
		String path = "";
		String directories = "";

		String domaindir = domain.replace("http://", "");
		domaindir = domaindir.replaceAll("/", "");
		path = domaindir + "\\";

		for (String part : urlparts) {
			if (!part.contains("."))
				directories += part + "\\";
			path += part + "\\";
		}

		//path = path.replace("%20", " ");

		directories = domaindir + directories;

		File f = new File(directories);
		f.mkdirs();

		System.out.println("dirs = " + directories);
		if (urlpath.equals("") || urlpath.equals("/"))
			path += "index.html";
		System.out.println("path = " + path);

		if (type.startsWith("text/html")) {
			html = loadHTML(url, path,domain);
		} else {
			loadFile(url, path);
		}
		return html;
	}

	/**
	 * Download een bestand
	 * 
	 * @param url
	 * @param destination
	 * @throws IOException
	 */
	public void loadFile(URL url, String destination) throws IOException {
		System.out.println("bestand downloaden ");
		System.out.println(url + " --> " + destination);
		URLConnection connection = url.openConnection();

		InputStream stream = connection.getInputStream();
		BufferedInputStream in = new BufferedInputStream(stream);
		FileOutputStream file = new FileOutputStream(destination);
		BufferedOutputStream out = new BufferedOutputStream(file);

		int i = 0;
		while ((i = in.read()) != -1) {
			out.write(i);
		}
		out.flush();
	}

	/**
	 * Download HTML naar ArrayList van Strings, elke regel 1 string
	 * 
	 * @param url
	 * @param destination
	 * @throws IOException
	 */
	public ArrayList<String> loadHTML(URL url, String destination,String domain) throws IOException {

		ArrayList<String> htmlList = new ArrayList<String>();
		URLConnection connection = url.openConnection();

		InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());

		BufferedReader in = new BufferedReader(streamReader);
		String lineIn = in.readLine();
		while (lineIn != null) {
			lineIn = in.readLine();
			htmlList.add(lineIn);
		}

		saveHTML(htmlList, destination,domain);
		return htmlList;

	}

	public void saveHTML(ArrayList<String> html, String destination,String domain) throws IOException {

		FileWriter file = new FileWriter(destination);
		BufferedWriter out = new BufferedWriter(file);
		for (String line : html) {

			if (line != null) {
				line = line.replaceAll("src=\"" + domain, "src=\".");
				line = line.replaceAll("href=\"" + domain, "href=\".");
				out.write(line + "\n");
			}
		}
		out.flush();
	}

}
